#include "Ogre.h"
#include "OIS.h"

#include "MyGUI.h"
#include "MyGUI_OgrePlatform.h"

#include "GUIManager.h"

GUIManager::GUIManager() : mSceneMgr(), mWindow(), mPlatform(){ }
GUIManager::~GUIManager(){ }

bool GUIManager::Startup(Ogre::RenderWindow *window, Ogre::SceneManager *sceneMgr)
{
	mWindow = window;
	mSceneMgr = sceneMgr;

	mPlatform = new MyGUI::OgrePlatform();
	mPlatform->initialise(mWindow, mSceneMgr, "GUI");

	mGUI = new MyGUI::Gui();
	mGUI->initialise();
	
	MyGUI::ButtonPtr button = mGUI->createWidget<MyGUI::Button>("Button", 30, 30,
		300, 100, MyGUI::Align::Left | MyGUI::Align::Top, "Overlapped");
	button->setVisible(true);
	//mGUI->setVisiblePointer(false);
	return true;
}

void GUIManager::KeyboardEvent(OIS::KeyCode key, unsigned int text, bool Pressed)
{	
	if(Pressed)
		mGUI->injectKeyPress(MyGUI::KeyCode::Enum(key), text);
	else
		mGUI->injectKeyRelease(MyGUI::KeyCode::Enum(key));
}

void GUIManager::MouseEvent(const OIS::MouseEvent &arg, OIS::MouseButtonID id, unsigned int Pressed)
{
	switch(Pressed)
	{
	case 0:
		mGUI->injectMouseRelease(arg.state.X.abs, arg.state.Y.abs, MyGUI::MouseButton::Enum(id));
		break;
	case 1:
		mGUI->injectMousePress(arg.state.X.abs, arg.state.Y.abs, MyGUI::MouseButton::Enum(id));
		break;
	case 2:
		mGUI->injectMouseMove(arg.state.X.abs, arg.state.Y.abs, arg.state.Z.abs);
		break;
	default:
		break;
	}
}

void GUIManager::Shutdown()
{
	mGUI->shutdown();
	delete mGUI;
	mGUI = NULL;   

	mPlatform->shutdown();
	delete mPlatform;
	mPlatform = NULL;
}